package com.zhou.base.components.feign.cloud.config;

import com.zhou.base.components.feign.cloud.loadbalancer.MyBalancerLifecycle;
import com.zhou.base.components.feign.cloud.loadbalancer.MyZonePreferenceServiceInstanceListSupplier;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.loadbalancer.cache.LoadBalancerCacheManager;
import org.springframework.cloud.loadbalancer.config.LoadBalancerZoneConfig;
import org.springframework.cloud.loadbalancer.core.CachingServiceInstanceListSupplier;
import org.springframework.cloud.loadbalancer.core.RandomLoadBalancer;
import org.springframework.cloud.loadbalancer.core.ReactorLoadBalancer;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;

/**
 * @description:
 * @author xianfeng
 * @date 2023/11/25 20:01
 */
public class CustomLoadBalancerConfig {
    //spring.cloud.loadbalancer.zone=xf8085

    /**
     * 默认的根据zone来选择
     * @param context
     * @return
     */
//    @Bean
//    public ServiceInstanceListSupplier discoveryClientServiceInstanceListSupplier(
//            ConfigurableApplicationContext context) {
//        return ServiceInstanceListSupplier.builder()
//                .withDiscoveryClient()
//                .withBlockingDiscoveryClient()
//                .withCaching()
//                .withZonePreference()
//                .withHealthChecks()
//                .build(context);
//    }

    /**
     * 服务实例的负载均衡器
     * @param environment
     * @param loadBalancerClientFactory
     * @return
     */
    @Bean
    ReactorLoadBalancer<ServiceInstance> randomLoadBalancer(Environment environment,
                                                            LoadBalancerClientFactory loadBalancerClientFactory) {
        String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
        return new RandomLoadBalancer(loadBalancerClientFactory
                .getLazyProvider(name, ServiceInstanceListSupplier.class),
                name);
    }

    /**
     *服务实例的提供者
     * @param context
     * @return
     */
    @Bean
    public ServiceInstanceListSupplier myZonePreferenceServiceInstanceListSupplier(ConfigurableApplicationContext context) {
        LoadBalancerZoneConfig zoneConfig = context.getBean(LoadBalancerZoneConfig.class);
        ServiceInstanceListSupplier baseSupplier = ServiceInstanceListSupplier.builder().withBlockingDiscoveryClient()
                .build(context);
        ObjectProvider<LoadBalancerCacheManager> cacheManagerProvider = context
                .getBeanProvider(LoadBalancerCacheManager.class);
        if (cacheManagerProvider.getIfAvailable() != null) {
            baseSupplier = new CachingServiceInstanceListSupplier(baseSupplier, cacheManagerProvider.getIfAvailable());
        }
        MyZonePreferenceServiceInstanceListSupplier myZonePreferenceServiceInstanceListSupplier = new MyZonePreferenceServiceInstanceListSupplier(baseSupplier, zoneConfig);
        return myZonePreferenceServiceInstanceListSupplier;
    }

    @Bean
    public MyBalancerLifecycle myBalancerLifecycle() {
        return new MyBalancerLifecycle();
    }
}
